package com.essc.controller;

import com.essc.dto.UpdatePwd;
import com.essc.pojo.User;
import com.essc.service.UserService;
import com.essc.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;

@CrossOrigin
@Controller
@RequestMapping("/user")//区别于其他模块
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 陈畅润
     */
    //完善用户信息
    @PostMapping("/updateUserInfo")
    @ResponseBody
    public JsonResult updateUserInfo(@RequestBody User user) {
        return userService.updateUserInfo(user);
    }

    //查询并显示用户信息
    @RequestMapping("/findUserInfo/{uid}")
    @ResponseBody
    public JsonResult findUserInfo(@PathVariable("uid") int uid) {
        return userService.findUserById(uid);
    }

    //修改用户密码
    @RequestMapping("/updateUserPassword")
    @ResponseBody
    public JsonResult updateUserPassword(@RequestBody UpdatePwd updatePwd) {
        String newPwd = updatePwd.getNewPwd();
        String oldPwd = updatePwd.getOldPwd();
        Integer uid = updatePwd.getUid();
        JsonResult jsonResult = userService.updatePwdById(oldPwd, newPwd, uid);
        return jsonResult;
    }

    //充值 洪志成
    /*需要传入uid amount*/
    @PostMapping("/Recharge/{uid}/{amount}")
    @ResponseBody
    public JsonResult Recharge(@PathVariable("uid") Integer uid,
                               @PathVariable("amount") double amount){
        return userService.recharge(uid, amount);
    }

    //    处理头像上传
    @RequestMapping("/upload")
    @ResponseBody
    public JsonResult uploadAvatar(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "username") String username, HttpSession session) throws FileNotFoundException {
        return userService.uploadAvatar(file, username, session);
    }

}
